package com.b2b.controller.company;


import com.b2b.model.entity.company.CompanyFinancialFlow;
import com.b2b.model.receive.CompanyId;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;

import com.b2b.model.common.ResponseBean;
import com.b2b.service.CompanyFinancialFlowService;
import com.b2b.service.CompanyFinancialService;
import com.b2b.util.CompanyFinancialFlowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;

import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/companyFinancialFlow")
@PropertySource("classpath:config.properties")
public class CompanyFinancialFlowController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final CompanyFinancialFlowUtil companyFinancialFlowUtil;

    private final CompanyFinancialFlowService companyFinancialFlowService;

    private final CompanyFinancialService companyFinancialService;

    @Autowired
    public CompanyFinancialFlowController(CompanyFinancialFlowUtil companyFinancialFlowUtil, CompanyFinancialService companyFinancialService, CompanyFinancialFlowService companyFinancialFlowService){
        this.companyFinancialFlowService = companyFinancialFlowService;
        this.companyFinancialService = companyFinancialService;
        this.companyFinancialFlowUtil = companyFinancialFlowUtil;
    }

    @PostMapping("/add")
    public ResponseBean login(@RequestBody CompanyFinancialFlow companyFinancialFlow) {
        if (companyFinancialFlow.getCompanyId()==null){
            throw new CustomException("添加失败(Insert Failure)");
        }
        Integer count = companyFinancialFlowUtil.add(companyFinancialFlow);
        if (count<=0){
            throw new CustomException("添加失败(Insert Failure)");
        }

        return new ResponseBean(HttpStatus.OK.value(), "添加成功(Insert successful)", null);
    }


    @GetMapping("/getAll")
//    @RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean user(CompanyId companyDto) {
        if (companyDto.getPage() == null || companyDto.getRows() == null) {
            companyDto.setPage(1);
            companyDto.setRows(10);
        }
        PageHelper.startPage(companyDto.getPage(), companyDto.getRows());
        List<CompanyFinancialFlow> companyFinancialFlows = companyFinancialFlowUtil.getMy(companyDto.getCompanyId());
        PageInfo<CompanyFinancialFlow> selectPage = new PageInfo<CompanyFinancialFlow>(companyFinancialFlows);
        if (companyFinancialFlows == null) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

}
